package flink_demo;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author Spring_Hu
 * @date 2021/10/10 8:57
 */
public class Test {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment ete = StreamExecutionEnvironment.getExecutionEnvironment();
        ete.readTextFile("test/word.txt")
                .flatMap(new FlatMapFunction<String, String>() {
                    public void flatMap(String value, Collector<String> out) throws Exception {
                        for (String word : value.split(" ")) {
                            out.collect(word);
                        }
                    }
                }).map(new MapFunction<String, Tuple2<String,Long>>() {
            public Tuple2<String, Long> map(String value) throws Exception {
                return Tuple2.of(value,1L);
            }
        })
                .keyBy(0)
                .sum(1).print();

        ete.execute();

    }


}
